package com.sbwork.demo.controller;

import com.sbwork.base.controller.BaseController;
import com.sbwork.base.form.ReturnForm;
import com.sbwork.base.form.ReturnPage;
import com.sbwork.demo.form.HyProjectForm;
import com.sbwork.demo.searchForm.HyProjectSearchForm;
import com.sbwork.demo.service.HyProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 项目管理controller
 */
@Api(tags = "项目管理接口")
@Controller
@RequestMapping("demo/hy/project")
public class HyProjectController extends BaseController {
    @Resource
    private HyProjectService hyProjectService;

    /**
     * 按projectId进行查询
     * @param projectId 项目标识
     * @return 项目管理Form
     */
    @ApiOperation(value = "按projectId获取")
    @ApiImplicitParams(
        @ApiImplicitParam(name = "projectId", value = "项目标识", paramType = "query", required = true)
    )
    @PostMapping(value = "/get")
    @ResponseBody
    @RequiresPermissions("hy:project:query")
    public ReturnForm<HyProjectForm> get(@RequestParam("projectId") String projectId){
        HyProjectForm form = hyProjectService.getByProjectId(projectId);
        return new ReturnForm<>(form);
    }

    /**
     * 按searchForm获取项目管理列表
     * @param searchForm 项目管理searchForm
     * @return 项目管理列表（带用分页的信息）
     */
    @ApiOperation(value = "获取项目管理列表")
    @PostMapping(value = "/list")
    @ResponseBody
    @RequiresPermissions("hy:project:query")
    public ReturnForm<ReturnPage<HyProjectForm>> list(HyProjectSearchForm searchForm){
        List<HyProjectForm> result = hyProjectService.listPageBySearchForm(searchForm);
        return new ReturnForm<>(new ReturnPage<>(searchForm.getTotal(), result));
    }

    /**
     * 更新 项目管理 排序
     *
     * @param keyIds 要更新的keyIds
     * @return 更新结果
     */
    @ApiOperation(value = "更新 项目管理排序")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "keyIds[]", value = "主键标识集合", required = true, allowMultiple = true)
    })
    @PostMapping(value = "/updateOrder")
    @ResponseBody
    @RequiresPermissions("hy:project:edit")
    public ReturnForm<String> updateOrder(@RequestParam("keyIds") String[] keyIds) {
        hyProjectService.updateOrder(keyIds);
        return new ReturnForm<>("更新成功！");
    }

    /**
     * 更新项目管理
     * @param form 项目管理form
     * @return 更新后的结果form
     */
    @ApiOperation(value = "更新或者添加项目管理")
    @PostMapping(value = "/commit")
    @ResponseBody
    @RequiresPermissions("hy:project:edit")
    public ReturnForm<HyProjectForm> commit(HyProjectForm form){
        form =  hyProjectService.commit(form);
        return new ReturnForm<>(form);
    }

    /**
     * 删除项目管理
     * @param keyIds projectId集合
     * @return ReturnForm.用于表示是否成功删除。
     */
    @ApiOperation(value = "删除项目管理")
    @ApiImplicitParams(
        @ApiImplicitParam(name = "keyIds[]", value = "标识集合", required = true)
    )
    @PostMapping(value = "/remove")
    @ResponseBody
    @RequiresPermissions("hy:project:delete")
    public ReturnForm<String> remove(@RequestParam("keyIds") String[] keyIds){
        hyProjectService.remove(keyIds);
        return new ReturnForm<>("成功删除");
    }
}
